package com.example.boxueguapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.boxueguapplication.model.User;
import com.example.util.MD5Util;

public class RegisterActivity extends AppCompatActivity {
    private EditText username;
    private EditText pwd;
    private EditText pswAgain;
    private Button regButton;
    private String uservalue;
    private String pwdvalue;
    private String pwd_againvalue;
    private TextView titleBar;
    private RelativeLayout appBar;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }

    private void init() {
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterActivity.this.finish();
            }
        });
        appBar = (RelativeLayout) findViewById(R.id.app_bar);
        appBar.setBackgroundColor(Color.TRANSPARENT);
        titleBar = (TextView) findViewById(R.id.title_bar);
        titleBar.setText("注册");
        username = (EditText) findViewById(R.id.username);
        pwd = (EditText) findViewById(R.id.pwd);
        pswAgain = (EditText) findViewById(R.id.pwd_again);
        regButton = (Button) findViewById(R.id.regButton);
        regButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getEditValue();
                if (isUserName()) {
                    Toast.makeText(RegisterActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                } else if (isPwd()) {
                    Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (isPwdB()) {
                    Toast.makeText(RegisterActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                } else if (isPwdAB()) {
                    Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                } else if (isUserPwd()) {
                    Toast.makeText(RegisterActivity.this, "账户已存在", Toast.LENGTH_SHORT).show();
                } else {
                    register();
                }
            }
        });
    }

    private boolean isUserPwd() {
        String pass = getSharedPreferences("userinfo", MODE_PRIVATE).getString(uservalue, "");
        return !TextUtils.isEmpty(pass);
    }


    private void register() {
        SharedPreferences userinfo;
        userinfo = getSharedPreferences("userinfo", MODE_PRIVATE);
        SharedPreferences.Editor edit = userinfo.edit();
        edit.putString(uservalue, MD5Util.md5(pwdvalue));
        User user = new User(uservalue, MD5Util.md5(pwdvalue), R.drawable.default_icon, "问答精灵", 1, "问答精灵");
        user.save();
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        edit.apply();
        Intent intent = new Intent();
        intent.putExtra("username",uservalue);
        setResult(1,intent);
        finish();
    }

    private boolean isPwdAB() {
        return !pwdvalue.equals(pwd_againvalue);
    }

    private boolean isPwdB() {
        return TextUtils.isEmpty(pwd_againvalue);
    }

    private boolean isPwd() {
        return TextUtils.isEmpty(pwdvalue);
    }

    private boolean isUserName() {
        return TextUtils.isEmpty(uservalue);
    }

    private void getEditValue() {
        uservalue = username.getText().toString();
        pwdvalue = pwd.getText().toString();
        pwd_againvalue = pswAgain.getText().toString();
    }
}